2;
/**
 * create by young (young2f@outlook.com) on 2024/4/21
 */
'use strict';
const base = require('../base/controllerBase');
const _stringUtils = require('../utils/stringUtils');
const { code } = require('../base/modelBase');
const table = 'DicPic';

class DictController extends base {
  async createDict() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        table: { type: 'string' },
      };

      ctx.validate(model, body);
      const { code } = body;
      body.code = code ? code : _stringUtils.getCode('DIC');
      const { table } = body;
      const result = await ctx.app.dbs.insert(table, body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }


  async deleteDict() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        table: { type: 'string' },
      };

      ctx.validate(model, body);
      const { table, code, tenantCode } = body;
      const result = await ctx.app.dbs.deleteOne(table, { code, tenantCode });
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async updateDict() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },
        table: { type: 'string' },
      };

      ctx.validate(model, body);
      const { table, code, tenantCode } = body;
      const result = await ctx.app.dbs.updateOne(table, { code, tenantCode }, body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findDicts() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        table: { type: 'string' },
      };

      ctx.validate(model, body);
      ctx.body = await service['dicService'].findMany(body);
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async createPicDict() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        treeId: { type: 'string' },
      };

      ctx.validate(model, body);
      body.code = _stringUtils.getCode('DIC');

      const result = await ctx.app.dbs.insert(table, body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async updatePicDict() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' },

      };

      ctx.validate(model, body);
      const { tenantCode, code, } = body;
      const result = await ctx.app.dbs.updateOne(table, { code, tenantCode }, body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findPicDicts() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },

      };

      ctx.validate(model, body);
      const { tenantCode } = body;
      const result = await ctx.app.dbs.find(table, { tenantCode });

      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async uploadFile() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },

      };

      ctx.validate(model, body);
      const { tenantCode } = body;
      body.code = _stringUtils.getCode('FIL');
      const result = await ctx.app.dbs.insert('DicFile', body);

      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async deleteFile() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        code: { type: 'string' }
      };

      ctx.validate(model, body);
      const { tenantCode, code } = body;
      const result = await ctx.app.dbs.deleteOne('DicFile', { tenantCode, code });

      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findFileMany() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
        treeId: { type: 'string' },
      };

      ctx.validate(model, body);
      console.log('find file', body);
      const { tenantCode, treeId, filename } = body;
      filename ? body.filename = { $regex: filename } : undefined;
      console.log('find file', body);
      const result = await ctx.app.dbs.find('DicFile', body);

      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      console.error(e);
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }
}

module.exports = DictController;
